
package login.beans;


import java.io.Serializable;
import login.bussiness.ConectionBusiness;
import java.sql.Connection;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;


/**
 *
 * @author juan
 */
@ManagedBean
@SessionScoped

public class ConectionBean implements Serializable{
    
    private static final long serialVersionUID = 1L;
    public String user;
    private String password;
    private String host = "localHost:1521";
    public String database;
    private static String message;
    public Connection currentConnection;
    static String SUCCESSFUL = "successful";
    static String SUCCESSFULPAGE = "principalPage";
    static String LOGIN = "login";
    private boolean sysdba;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Connection getCurrentConnection() {
        return currentConnection;
    }

    public void setCurrentConnection(Connection currentConnection) {
        this.currentConnection = currentConnection;
    }

    public boolean isSysdba() {
        return sysdba;
    }

    public void setSysdba(boolean sysdba) {
        this.sysdba = sysdba;
    }
    
    public String login() throws SQLException {
        
        ConectionBusiness conectionBusiness;
        if( !sysdba )
            conectionBusiness = new ConectionBusiness(user, password,host,database);
        else 
            conectionBusiness = new ConectionBusiness("sys as sysdba", password, host, database);
        
        String result = conectionBusiness.login( this );
        
        if( result.equals(SUCCESSFUL) ){
            HttpSession session = (HttpSession)FacesContext.
                                                getCurrentInstance().
                                                  getExternalContext().
                                                    getSession(false);
            
            if( sysdba )
                user = user+ " as sysdba";
                
            session.setAttribute("username", user);
            session.setAttribute("host", host);
            session.setAttribute("SDI", database);
            message = SUCCESSFULPAGE;
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Bienvenido!",
                    user));
        }else{
            
            message = LOGIN;
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Please Try Again!",
                    result));
        }
        
        return message;
        
    }
    
    public String logout() {
        
        try {
            currentConnection.close();
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Error al Cerrar Sesion!",
                    "Cierrela manualmentes"));
        }
        HttpSession session = (HttpSession)
                                FacesContext.
                                getCurrentInstance().
                                getExternalContext().
                                getSession(false);
        session.invalidate();       
        return "/"+LOGIN;
    }
            
}
